package com.example.demo.controller;

import com.example.demo.conf.GirlProperties;
import com.example.demo.conf.GirlRepository;
import com.example.demo.entity.girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @创建者：zzw
 * @创建时间 2018/5/28 0028
 * @描述
 */
@RestController
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @Autowired
    private GirlRepository girlRepository;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(@RequestParam(value = "id",defaultValue = "0",required = false) Integer Id){
         return "id:"+Id;
//         return girlProperties.getCupSize();
    }

    @GetMapping(value = "/getAll")
    public List<girl> girlList(){
        return girlRepository.findAll();
    }

    @PostMapping(value = "/addGirl")
    public girl add(@RequestParam("name") String name,
                     @RequestParam("age") Integer age,
                     @RequestParam("cupSize") String cupSize){
        girl g = new girl();
        g.setAge(age);
        g.setCupSize(cupSize);
        g.setName(name);
        return girlRepository.save(g);

    }

    @GetMapping(value = "/girls/{id}")
    public girl findGirlOne(@PathVariable("id") Integer id){
        return girlRepository.findById(id).get();
    }

    @PutMapping(value = "/girls/{id}")
    public girl updateGirl(@PathVariable("id") Integer id,
                           @RequestParam("name") String name,
                           @RequestParam("age") Integer age,
                           @RequestParam("cupSize") String cupSize){
        girl g = new girl();
        g.setId(id);
        g.setName(name);
        g.setCupSize(cupSize);
        g.setAge(age);
        return girlRepository.save(g);
    }

    @DeleteMapping(value = "/girls/{id}")
    public void delGirl(@PathVariable("id") Integer id){
        girlRepository.deleteById(id);
    }

    @GetMapping(value = "/girls/age/{age}")
    public List<girl> findByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
}
